using DG.Tweening;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace IQIGame.Onigao.GamePlay
{
    public class UIMain : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIMain),
            layer = EUILayer.Main,
            permanent = true,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected class UIB_UIMain
        {
            #region ObjectBinding Generate
            public RectTransform topRightBtns { protected set; get; }
            public ExButton btnStorage { protected set; get; }
            public ExImage headIcon { protected set; get; }
            public ExImage frameImage { protected set; get; }
            public ExButton btnFunctionTest { protected set; get; }
            public ExButton btnTime { protected set; get; }
            public GameObject miniMap { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<RectTransform>("topRightBtns", out var __tbv0);
                topRightBtns = __tbv0;
                __binding.TryGetVariableValue<ExButton>("btnStorage", out var __tbv1);
                btnStorage = __tbv1;
                __binding.TryGetVariableValue<ExImage>("headIcon", out var __tbv2);
                headIcon = __tbv2;
                __binding.TryGetVariableValue<ExImage>("frameImage", out var __tbv3);
                frameImage = __tbv3;
                __binding.TryGetVariableValue<ExButton>("btnFunctionTest", out var __tbv4);
                btnFunctionTest = __tbv4;
                __binding.TryGetVariableValue<ExButton>("btnTime", out var __tbv5);
                btnTime = __tbv5;
                __binding.TryGetVariableValue<GameObject>("miniMap", out var __tbv6);
                miniMap = __tbv6;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        protected UIB_UIMain ui { get; set; }

        protected override void BeforeInit()
        {
            ui = new UIB_UIMain();
            ui.InitBinding(csObjBind);
        }

        bool isShowTopBtn;
        int cntTopBtn;
        int indexTopBtn;
        bool isTopBtnInEffect;
        private UIN_MiniMap _miniMap;
        Dictionary<int, ExButton> topRightBtns = new(4);

        protected override void OnInit()
        {
            cntTopBtn = (int)UIMainTabType.MaxLength;
            for (int i = 0; i < cntTopBtn; i++)
            {
                BindBtnDynamicData(i);
            }

            isUpdate = true;
            _miniMap = InitChildNode<UIN_MiniMap>(ui.miniMap);
            _miniMap.Show();
            ui.btnFunctionTest.AddClickEvent(OnClickBtnFunctionTest);
            ui.btnStorage.AddClickEvent(OnClickStorage);
            ui.btnTime.AddClickEvent(OnClickBtnTime);
        }

        protected override void OnDispose()
        {
        }

        protected override void OnShow(UIBaseData data)
        {
            if (GameDataCenter.PlayerData == null)
                return;
            var roleCfg = TableCenter.role.Get(GameDataCenter.PlayerData.roleCid);
            SetImageSprite(ui.headIcon, roleCfg.HeadImage_fullPath);
        }

        protected override void OnHide()
        {

        }

        void BindBtnDynamicData(int emIntValue)
        {
            UIMainTabType data = (UIMainTabType)emIntValue;
            if (!topRightBtns.TryGetValue(emIntValue, out ExButton btn))
            {
                btn = ui.topRightBtns.Find<ExButton>("Btn" + data);
                topRightBtns[emIntValue] = btn;
            }

            UnityAction onClick = GetBtnFunc(data);
            if (btn != null)
            {
                btn.SetActive(isShowTopBtn);
                if (onClick != null)
                    btn.onClick.AddListener(onClick);
            }
        }

        UnityAction GetBtnFunc(UIMainTabType btnType)
        {
            return btnType switch
            {
                UIMainTabType.Hero => OnClickHero,
                UIMainTabType.Formation => OnClickFormation,
                UIMainTabType.Warehouse => OnClickWarehouse,
                UIMainTabType.Quest => OnClickQuest,
                UIMainTabType.Mail => OnClickMail,
                UIMainTabType.Equip => OnClickEquip,
                UIMainTabType.Mall => OnClickMall,
                UIMainTabType.Pet => OnClickPet,
                UIMainTabType.Friend => OnClickFriend,
                UIMainTabType.Setting => OnClickSetting,
                _ => null,
            };
        }


        void OnClickHero()
        {
            if (FunctionModule.CheckIsOpenAndTip(FunctionType.HeroList))
                OpenUI<UIHeroList>();
        }

        void OnClickFormation()
        {
            if (FunctionModule.CheckIsOpenAndTip(FunctionType.Formation))
                OpenUI<UIFormation>();
        }

        void OnClickStorage()
        {
            if (isTopBtnInEffect && cntTopBtn > 0)
                return;
            isTopBtnInEffect = true;
            bool isCurr = !isShowTopBtn;
            isShowTopBtn = isCurr;
            indexTopBtn = 0;

            var dt = DOTween.Sequence().SetLink(gameObject).SetTarget(ui.btnStorage)
            .AppendInterval(0.04f)
            .OnUpdate(OnUpdateTweenTopBtn)
            .SetLoops(cntTopBtn)
            .OnStepComplete(OnStepCompleteTweenTopBtn)
            .OnComplete(OnCompleteTweenTopBtn);
            dt.Play();

            TweenViewBtn();
            UIUtil.SetFlip(ui.btnStorage.background.transform, isShowTopBtn);
        }

        void TweenViewBtn()
        {
            topRightBtns.TryGetValue(indexTopBtn, out ExButton btn);
            btn?.SetActive(isShowTopBtn);
        }

        void OnUpdateTweenTopBtn()
        {
        }

        void OnStepCompleteTweenTopBtn()
        {
            indexTopBtn++;
            TweenViewBtn();
        }

        void OnCompleteTweenTopBtn()
        {
            isTopBtnInEffect = false;
            indexTopBtn = 0;
        }

        void OnClickWarehouse()
        {
            if (FunctionModule.CheckIsOpenAndTip(FunctionType.Warehouse))
                OpenUI<UIWarehouse>();
        }

        void OnClickBtnFunctionTest()
        {
            OpenUI<UIFunctionTest>();
        }

        void OnClickQuest()
        {
            if (FunctionModule.CheckIsOpenAndTip(FunctionType.Quest))
                OpenUI<UIQuest>();
        }

        void OnClickEquip()
        {
            if (FunctionModule.CheckIsOpenAndTip(FunctionType.Equip))
            {
                OpenUI<UIEquipList>(UD_EquipList.NewFromPool());
            }
        }

        void OnClickMail()
        {
            if (FunctionModule.CheckIsOpenAndTip(FunctionType.Mail))
                OpenUI<UIMail>();
        }

        void OnClickMall()
        {
            //常驻商城
            if (FunctionModule.CheckIsOpenAndTip(FunctionType.Mall))
                OpenUI<UIMallNormal>();
        }
        void OnClickFriend()
        {
            if (FunctionModule.CheckIsOpenAndTip(FunctionType.Friend))
                OpenUI<UIFriend>();
        }

        void OnClickPet()
        {
            if (FunctionModule.CheckIsOpenAndTip(FunctionType.Pet))
                OpenUI<UIPetWarehouse>();
        }

        void OnClickSetting()
        {
            OpenUI<UISetting>();
        }

        private void OnClickBtnTime()
        {
            OpenUI<UIChangeTime>();
        }
    }
}

